<?php
/**
 * Description of SubCategory
 *
 * @author Admin
 */
class Category_Resource_Subcategory extends WOW_Model_Resource_Db_Table_Abstract implements
Category_Resource_Subcategory_Interface
{
    protected $_name = 'asset_subcategories';
    protected $_primary = 'subcatid';
    protected $_rowClass = 'Category_Resource_Subcategory_Item';

    protected $_dependentTables = array('Category_Resource_Label');

    protected $_referenceMap = array(
        'Category' => array(
            'columns' => 'catid',
            'refTableClass' => 'Category_Resource_Category',
            'refColumns' => 'catid',
        )
    );

    public function getSubcategoryById($id)
    {
        return $this->find($id)->current();
    }

    public function getAllSubcategories()
    {
        $select = $this->select()
                       ->order('SubCategoryName');

        return $this->fetchAll($select);
    }

    public function getSubcategoryByIdent($ident)
    {
        $select = $this->select()
                       ->where('subcat_ident = ?', $ident);

        return $this->fetchRow($select);
    }

    public function getSubcategoriesByParentId($id)
    {
         $select = $this->select()
                       ->where('catid = ?', $id);
          return $this->fetchAll($select);
    }

    /**
     * finds the subcategories by parent category ident
     * @param String $ident
     * @return Category_Resource_Subcategory
     */
    public function getSubcategoriesByParentIdent($ident)
    {
        // find parent category ID by using $ident
        // then find all the subcategories by parent category ID
        $cat_model = new Category_Resource_Category();
        $cat = $cat_model->getCategoryByIdent($ident);
        return $this->getSubcategoriesByParentId($cat->catid);
    }
    
    /**
     * find the parent category either by sub-category id or by sub-category ident
     * @param int $subcatid
     * @param String $subcat_ident
     * @return Category_Resource_Category | if $subcatid & $subcat_ident are null returns false
     */
    public function getParentCategory($subcatid=null,$subcat_ident=null)
    {
       // $cat_model = new Category_Resource_Category();
        if($subcatid != null)
        {
            //echo '<br> getSubcategoryById  :: ';
            $subcat = $this->getSubcategoryById($subcatid);
        }
        else if ($subcat_ident != null)
        {
            //echo '<br> getSubcategoryByIdent  :: ';
             $subcat = $this->getSubcategoryByIdent($subcat_ident);
        }
        else
        {
            //echo '<br> returning false  :: ';
            return false;
        }
        return $subcat->findParentRow('Category_Resource_Category');
    }
    
}
?>
